package com.ydche.service.impl;

import com.ydche.dao.IAgentDao;
import com.ydche.service.IAgentService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AgentServiceImpl implements IAgentService {

    @Autowired
    private IAgentDao agentDao;

    private static Logger log = LogManager.getLogger(AgentServiceImpl.class);

    @Override
    public List<Map<String, Object>> getMyAgentInfo(long userId) {
        log.info("getMyAgentInfo-------------,useId="+userId);
        List<Map<String, Object>> list=new ArrayList<>();
        Map<String, Object> map=new HashMap<String, Object>();
        Map<String, Object> params=new HashMap<String, Object>();
        Map<String, Object> info = agentDao.agentOwner(userId);
        if (info != null) {
            params.put("id", info.get("user_id"));
            params.put("name", info.get("alias_name"));
            map.put("my_superior", params);
        } else {
            map.put("my_superior", params);
        }
        List<Map<String, Object>> agent1listmap = agentDao.myAgentByLevel(userId,1);
        List<Map<String, Object>> agent2listmap = agentDao.myAgentByLevel(userId,2);
        List<Map<String, Object>> agent3listmap = agentDao.myAgentByLevel(userId,3);
        List<Map<String, Object>> listone=new ArrayList<>();
        for (Map<String, Object> agent1 : agent1listmap) {
            List<Map<String, Object>> listtwo = new ArrayList<>();
            for (Map<String, Object> agent2 : agent2listmap) {
                if (agent1.get("id").toString().equals(agent2.get("ownerId").toString())){
                    List<Map<String, Object>> listthree = new ArrayList<>();
                    for (Map<String, Object> agent3 : agent3listmap){
                        if (agent2.get("id").toString().equals(agent3.get("ownerId").toString())){
                            listthree.add(agent3);
                        }
                    }
                    agent2.put("agency", listthree);
                    listtwo.add(agent2);
                }
            }
            agent1.put("agency", listtwo);
            listone.add(agent1);
        }
        map.put("agency", listone);
        list.add(map);
        return list;
    }

    @Override
    public Map<String, Object> agentInfo(long userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> info = agentDao.agentOwner(userId);
        if (info != null) {
            map.put("myprivateagency_id", info.get("user_id"));
            map.put("myprivateagency", info.get("alias_name"));
        } else {
            map.put("myprivateagency_id", "");
            map.put("myprivateagency", "");
        }
        map.put("onestepagency", agentDao.myAgentByLevel(userId,1));
        map.put("twostepagency", agentDao.myAgentByLevel(userId,2));
        map.put("threestepagency", agentDao.myAgentByLevel(userId,3));
        return null;
    }

    @Override
    public boolean updateNickName(long userId, long customerId, String nickName) {
        Map<String, Object> nickInfo = agentDao.getCustomerNickInfo(userId, customerId);
        if (nickInfo.size()==0){
            agentDao.addCustomerNickInfo(userId, customerId, nickName);
        }else{
            agentDao.updateCustomerNickInfo(userId, customerId, nickName);
        }
        return true;
    }
}
